package com.lu.sb210207.components;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

@Component
public class AES {


    /**
     * 生成key，作为加密和解密密钥且只有密钥相同解密加密才会成功
     *
     * @return
     */
    public static Key createKey() {

        try {
            // 生成key
            KeyGenerator keyGenerator;
            //构造密钥生成器，指定为AES算法,不区分大小写
            keyGenerator = KeyGenerator.getInstance("AES");
            //生成一个128位的随机源,根据传入的字节数组
            keyGenerator.init(128);
            //产生原始对称密钥
            SecretKey secretKey = keyGenerator.generateKey();
            //获得原始对称密钥的字节数组
            byte[] keyBytes = secretKey.getEncoded();
            // key转换,根据字节数组生成AES密钥
            Key key = new SecretKeySpec(keyBytes, "AES");

            return key;

        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();

            return null;
        }

    }

    /**
     * 加密
     *
     * @param context 需要加密的明文
     * @param key     加密用密钥
     * @return
     */
    public static byte[] jdkAES(String context, Key key) {

        try {

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

            cipher.init(cipher.ENCRYPT_MODE, key);
            //将加密并编码后的内容解码成字节数组
            byte[] result = cipher.doFinal(context.getBytes());

            System.out.println("jdk aes:" + Base64.encode(result));

            return result;

        } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException e) {

            e.printStackTrace();

            return null;
        }
    }

    /**
     * 解密
     *
     * @param result 加密后的密文byte数组
     * @param key    解密用密钥
     */
    public static void decrypt(byte[] result, Key key) {

        Cipher cipher;

        try {

            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            //初始化密码器，第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作，第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);

            result = cipher.doFinal(result);

        } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {

            e.printStackTrace();
        }

        System.out.println("jdk aes desrypt:" + new String(result));
    }


    @Bean
    public void init() {

        System.out.println("com/lu/sb210207/components/AES.java");

        Key key = createKey();

        // 加密
        byte[] bytes = jdkAES("admin0818", key);
        // 解密
        decrypt(bytes, key);
    }

}
